import 'dart:io' show Platform;

import 'package:device_info/device_info.dart';
import 'package:utils/plugins/flutter_utils.dart';

import 'Print.dart';

class DeviceUtil {
  DeviceUtil._();

  /// TODO 手机型号
  static Future<String> getMode() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
      return androidDeviceInfo.model;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      return iosDeviceInfo.utsname.machine;
    }
    return "";
  }

  /// TODO 当前运行设备
  /// 返回值：
  ///   1：表示运行在手机上
  ///   2：表示运行在平板上
  static Future<String> getPhoneType() async {
    int phoneType = await FlutterUtils.getPhoneType();
    Print().printNative("getPhoneType ($phoneType)");
    if (phoneType == -1) {
      return "";
    }
    if (phoneType == 0) {
      return "2";
    }
    return "1";
  }

  /// TODO 系统版本
  static Future<String> getSystemVersion() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
      return androidDeviceInfo.version.release;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      return iosDeviceInfo.systemVersion;
    }
    return "";
  }

  static Future<String> getDeviceLanguage() async {
    return await FlutterUtils.getLanguage();
  }

  /// TODO 设备随机数
  static Future<String> getSerial() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      String imei = await FlutterUtils.getImei();
      if (imei?.isNotEmpty == true) {
        return imei;
      }
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
      return androidDeviceInfo.androidId;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      return iosDeviceInfo.identifierForVendor;
    }
    return "";
  }
}
